home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / clipplanes / clipplanes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.1 KB  |  224 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* clipplanes.c
  19.  *     This program demonstrates how to define and use arbitrary 
  20.  *     clipping planes.
  21.  *
  22.  *     ESCAPE key    - exits program
  23.  *     SPACE key    - toggles fixed/moving clipping planes
  24.  */
  25. #include <GL/gl.h>
  26. #include <GL/glu.h>
  27. #include <GL/glut.h>
  28.  
  29. #include <math.h>
  30. #include <stdio.h>
  31.  
  32. /* Function Prototypes */
  33.  
  34. GLvoid  initgfx( GLvoid );
  35. GLvoid  drawScene( GLvoid );
  36. GLvoid  reshape( GLsizei, GLsizei );
  37. GLvoid  animate( GLvoid );
  38. GLvoid  visibility( GLint );
  39. GLvoid  keyboard( GLubyte, GLint, GLint );
  40.  
  41. GLvoid  drawTorusRing( GLboolean );
  42.  
  43. void printHelp( char * );
  44.  
  45. /* Global Definitions */
  46.  
  47. #define KEY_ESC    27    /* ascii value for the escape key */
  48.  
  49. /* Global Variables */
  50.  
  51. static GLboolean movingClipPlanes = GL_TRUE;
  52.  
  53. static GLfloat angle = 0.0; 
  54.  
  55. GLvoid
  56. main( int argc, char *argv[] )
  57. {
  58.     GLsizei  width, height, winSize;
  59.  
  60.     glutInit( &argc, argv );
  61.  
  62.     width = glutGet(GLUT_SCREEN_WIDTH); 
  63.     height = glutGet(GLUT_SCREEN_HEIGHT);
  64.     winSize = ( width < height ) ? width : height;
  65.     glutInitWindowPosition( winSize/4, winSize/4 ); 
  66.     glutInitWindowSize( winSize/2, winSize/2 );
  67.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  68.     glutCreateWindow( argv[0] );
  69.     
  70.     initgfx();
  71.  
  72.     glutKeyboardFunc( keyboard );
  73.     glutReshapeFunc( reshape );
  74.     glutIdleFunc( animate ); 
  75.     glutVisibilityFunc( visibility ); 
  76.     glutDisplayFunc( drawScene ); 
  77.  
  78.     printHelp( argv[0] );
  79.     glutMainLoop();
  80. }
  81.  
  82. GLvoid
  83. printHelp( char *progname )
  84. {
  85.     fprintf(stdout, "\n%s - demonstrates how to define and use \n"
  86.         "arbitrary clipping planes.\n\n"
  87.         "SPACE key    - toggles fixed/moving clipping planes \n"
  88.         "Escape key    - exit the program \n\n",
  89.         progname );
  90. }
  91.  
  92.  
  93. void
  94. initgfx( void )
  95. {
  96.     GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
  97.     GLfloat mat_shininess[] = { 10.0 };
  98.  
  99.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  100.  
  101.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  102.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  103.  
  104.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  105.  
  106.     glEnable( GL_LIGHTING );
  107.     glEnable( GL_LIGHT0 );
  108.  
  109.     glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
  110.     glEnable( GL_COLOR_MATERIAL );
  111.  
  112.     glClearColor( 0, 0, 0, 1 );
  113.     glEnable( GL_DEPTH_TEST );
  114. }
  115.  
  116. GLvoid 
  117. keyboard( GLubyte key, GLint x, GLint y )
  118. {
  119.     switch (key) {
  120.     case ' ':    /* toggle moving clipping plane on and off */
  121.         movingClipPlanes = !movingClipPlanes;    
  122.         glutPostRedisplay();
  123.         break;
  124.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  125.         exit(0);
  126.     }
  127. }
  128.  
  129. GLvoid
  130. reshape( GLsizei width, GLsizei height )
  131. {
  132.     GLdouble    aspect;
  133.  
  134.     glViewport( 0, 0, width, height );
  135.  
  136.     aspect = (GLdouble) width / (GLdouble) height;
  137.  
  138.     glMatrixMode( GL_PROJECTION );
  139.     glLoadIdentity();
  140.     gluPerspective( 45.0, aspect, 3.0, 13.0 );
  141.     glMatrixMode( GL_MODELVIEW );
  142.     glLoadIdentity();
  143.     glTranslatef( 0.0, 0.0, -8.0 ); 
  144. }
  145.  
  146. GLvoid
  147. animate( GLvoid )
  148. {
  149.     /* update the current angle */
  150.     angle = fmodf( (angle - 1.0), 360.0 );
  151.  
  152.     /* Tell GLUT to redraw the scene */
  153.     glutPostRedisplay();
  154. }
  155.         
  156. GLvoid
  157. visibility( int state )
  158. {
  159.     if (state == GLUT_VISIBLE) {
  160.         glutIdleFunc( animate );
  161.     } else {
  162.         glutIdleFunc( NULL );
  163.     }
  164. }
  165.  
  166. GLvoid
  167. drawTorusRing( GLboolean solid )
  168. {
  169.     int i,  slices = 8;
  170.     for ( i = 0; i < slices; i++ )
  171.     {
  172.         glColor3f( i/10.0, i/10.0, 1.0 - i/10.0 ); 
  173.         glPushMatrix(); 
  174.             glRotatef( i * 360.0/slices, 0, 0, 1 );
  175.             glTranslatef( 1.5, 0.0, 0.0 );
  176.             glRotatef( i * 360.0/slices, 0, 1, 0 );
  177.             if (solid) 
  178.                 glutSolidTorus( 0.25, 0.75, 8, 15 );
  179.             else 
  180.                 glutWireTorus( 0.25, 0.75, 8, 15 );
  181.         glPopMatrix();
  182.     }
  183. }
  184.  
  185. GLvoid
  186. drawScene( GLvoid )
  187. {
  188.     GLdouble planeABCD[4] = { -1.0, 0.0, 0.0, 0.5 };
  189.  
  190.     /* to reverse clipping direction, reverse signs of A, B, C, & D */ 
  191.     GLdouble oppositePlaneABCD[4]  = { 1.0, 0.0, 0.0, -0.5 };
  192.  
  193.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  194.  
  195.     if ( !movingClipPlanes ) {
  196.         /* define clipping planes fixed in scene */
  197.         glClipPlane (GL_CLIP_PLANE0, planeABCD);
  198.         glClipPlane (GL_CLIP_PLANE1, oppositePlaneABCD);
  199.     }
  200.     
  201.     glPushMatrix(); 
  202.         glRotatef( angle, 0.0, 1.0, 0.0 );
  203.         
  204.         if ( movingClipPlanes ) {
  205.             /* defining clipping planes after rotation
  206.              * so they stay fixed relative to the objects */
  207.             glClipPlane (GL_CLIP_PLANE0, planeABCD);
  208.             glClipPlane (GL_CLIP_PLANE1, oppositePlaneABCD);
  209.         }
  210.  
  211.         /* clip parts of the solid torii */
  212.         glEnable (GL_CLIP_PLANE0);
  213.         drawTorusRing( GL_TRUE );
  214.         glDisable (GL_CLIP_PLANE0);
  215.  
  216.         /* clip the opposite parts of the wire torii */
  217.         glEnable (GL_CLIP_PLANE1);
  218.         drawTorusRing( GL_FALSE );
  219.         glDisable (GL_CLIP_PLANE1);
  220.     glPopMatrix();
  221.  
  222.     glutSwapBuffers();
  223. }
  224.